package com.cloudcc.mobile.view.fragment;

import android.view.View;
import android.widget.ListView;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.bus.MenuToggleEventR;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicDefaultHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.PushSaveMessage;
import com.cloudcc.mobile.event.BeauEventList;
import com.cloudcc.mobile.messagecent.BeauTongzhi;
import com.cloudcc.mobile.messagecent.TongzhiImpl;
import com.cloudcc.mobile.view.base.CFragment;
import com.cloudcc.mobile.view.base.SlidingRightAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/9/4.
 */

public class MenuRightFragment extends CFragment implements PtrHandler,IEventLife{
    public PtrClassicFrameLayout xlsx_x_bsrs;
    private ListView xmListview;
    private List<PushSaveMessage> messsageList;

    public SlidingRightAdapter tzadapter_x;
    private TongzhiImpl tongzhiImpl = new TongzhiImpl();
    private BeauEventList.BeauTongzhiEvent event;
    @Override
    public void init() {
        xlsx_x_bsrs = (PtrClassicFrameLayout)findViewById(R.id.xlsx_x_bsr);
        xmListview = (ListView)findViewById(R.id.messagee_lv_x);
        register();
        initRefreshTz();
        initMessage();
    }

    @Override
    public int getLayoutId() {
        return R.layout.right_menu_sl;
    }
    private void initMessage(){
//        PushSaveMessageDB db = new PushSaveMessageDB(mContext, UserManager
//                .getManager().getUser().userId + "pushsavemessage");
//        try {
//            messsageList = db.findAllPushSaveMessage(PushSaveMessage.class);
//            if(messsageList==null) {
//                messsageList = new ArrayList<>();
//            }else{
//                Collections.reverse(messsageList);
//            }
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
            event = new BeauEventList.BeauTongzhiEvent();
            tongzhiImpl.tongzhiDate(event);

    }
    private void initRequestDataTz() {
        xlsx_x_bsrs.postDelayed(new Runnable() {
            @Override
            public void run() {
                xlsx_x_bsrs.autoRefresh(true);
            }
        }, 150);
    }
    private void initRefreshTz() {
        xlsx_x_bsrs.setLoadingMinTime(1000);
        xlsx_x_bsrs.setPtrHandler(this);
        PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(mContext);
        header.setbg();
        xlsx_x_bsrs.setHeaderView(header);
        xlsx_x_bsrs.addPtrUIHandler(header);

    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame,xmListview,header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        initMessage();
//        tzadapter_x = new SlidingRightAdapter(messsageList,mContext);
//        xmListview.setAdapter(tzadapter_x);
//        xlsx_x_bsrs.refreshComplete();
    }

    @Override
    public void onResume() {
        super.onResume();
        initRequestDataTz();
    }

    @Override
    public void register() {
        EventEngine.register(this);
    }

    @Override
    public void unRegister() {
        EventEngine.uregister(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegister();
    }

    public void onEventMainThread(MenuToggleEventR event) {
        initRequestDataTz();
    }

    public void onEventMainThread(BeauEventList.BeauTongzhiEvent event) {
        if (event.isError()) {
            return;
        }
        List<BeauTongzhi> tongzhiLi = event.getData();
        tzadapter_x = new SlidingRightAdapter(tongzhiLi,mContext);
        xmListview.setAdapter(tzadapter_x);
        xlsx_x_bsrs.refreshComplete();

    }
}